package com.geekqx;

import com.geekqx.Utils.FileUtil;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

/**
 * pdf解析
 *
 * @author kevin
 * @version 1.0
 * @date 2017-05-10 15:36
 */
public class PdfReader {
    public static void main(String[] args) throws IOException {
        String path = PdfReader.class.getResource("/").getPath();
        String pdfPath = path + "JBT 4171-1999 木工机床 精度检验通则.pdf";
        String content = parsingPdf(pdfPath);

        FileUtil f = new FileUtil();
        f.WriteFile(content, path, "JBT 4171-1999 木工机床 精度检验通则.txt", "UTF-8");

        parsingPdfImg(pdfPath, path);

    }

    public static String parsingPdf(String pdfPath) {
        File pdfFile = new File(pdfPath);
        String content = "";
        PDDocument document = null;
        try {
            // 方式一：
            /**
             InputStream input = null;
             input = new FileInputStream( pdfFile );
             //加载 pdf 文档
             PDFParser parser = new PDFParser(new RandomAccessBuffer(input));
             parser.parse();
             document = parser.getPDDocument();
             **/

            // 方式二：
            document = PDDocument.load(pdfFile);

            // 获取页码
            int pages = document.getNumberOfPages();

            // 读文本内容
            PDFTextStripper stripper = new PDFTextStripper();
            // 设置按顺序输出
            stripper.setSortByPosition(true);
            stripper.setStartPage(1);
            stripper.setEndPage(pages);
            content = stripper.getText(document);
            System.out.println(content);
        } catch (Exception e) {
            System.out.println(e);
        }
        return content;
    }

    public static void parsingPdfImg(String pdfPath, String outImgPath) throws IOException {
        // 待解析PDF
        File pdfFile = new File(pdfPath);
        PDDocument document = null;
        try {
            document = PDDocument.load(pdfFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int pages_size = document.getNumberOfPages();

        System.out.println("getAllPages===============" + pages_size);
        int j = 0;

        for (int i = 0; i < pages_size; i++) {
            // 读文本内容
            PDFTextStripper stripper = new PDFTextStripper();
            // 设置按顺序输出
            stripper.setSortByPosition(true);
            stripper.setStartPage(i+1);
            stripper.setEndPage(i+1);
            String content = stripper.getText(document);
            System.out.println(content);



            PDPage page = document.getPage(i);
            PDResources resources = page.getResources();
            Iterable xobjects = resources.getXObjectNames();

            if (xobjects != null) {
                Iterator imageIter = xobjects.iterator();
                while (imageIter.hasNext()) {
                    COSName key = (COSName) imageIter.next();
                    if (resources.isImageXObject(key)) {
                        try {
                            PDImageXObject image = (PDImageXObject) resources.getXObject(key);

                            //将PDF文档中的图片 分别另存为图片。
                            File file = new File(outImgPath + "img_" + j + ".png");

                            BufferedImage input = image.getOpaqueImage();

                            ImageIO.write(input, "png", file);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        j++;
                    }
                }
            }
        }
        try{
            document.close();
        }catch (Exception e){
            System.out.println(e);
        }
        System.out.println(j);
    }

    public static void createBlankPdf(String outPdfPath) {
        PDDocument document = new PDDocument();
        try {

            PDPage page = new PDPage();
            document.addPage(page);
            PDFont font = PDType1Font.HELVETICA_BOLD;

            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.beginText();
            contentStream.setFont(font, 14);
//            contentStream.moveTextPositionByAmount(100, 700);
//            contentStream.drawString("Hello World");
//            contentStream.drawString("中文");
            contentStream.endText();

            contentStream.close();


            document.save(outPdfPath);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
